package com.moon.game.proxy;

import com.moon.game.constants.Constants;
import com.moon.game.exception.GException;
import com.moon.game.message.ObjMessage;
import com.moon.game.util.GDateUtils;

public class ProxyHandler {
	
	/**
	 * 获取指定位置字符串
	 * 
	 * @param uri
	 * @param index 指定位置,从1开始
	 * @return
	 * @throws GException 
	 */
	private static String parseURI(String uri, int index) throws GException {
		if (uri.startsWith("[/]")) {
			uri = uri.substring(1, uri.length());
		}
		String[] context = uri.split("[/]");
		if (index > context.length) {
			throw new GException("input index ArrayIndexOutOfBounds");
		}
		return context[index];
	}
	
	/**
	 * 操作超时检测
	 * 
	 * @param uri
	 * @return
	 */
	public static boolean checkTime(String uri) {
		try {
			String security = parseURI(uri, Constants.SECURITY_PLACE);
			String[] info = security.split("[-]");
			if (info == null || info.length < 2) {
				return false;
			}
			int time = Integer.parseInt(info[1]);
			if (GDateUtils.now() - time > Constants.MAKE_OUT_OF_DATA) {
				//15分钟没有操作设置书签过期
				return false;
			}
		} catch (GException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 获取cmd
	 * 
	 * @param uri
	 * @return
	 */
	public static String getCmd(String uri) {
		try {
			String par = parseURI(uri, Constants.PARAMETER_PLACE);
			String[] info = par.split("[?]");
			String cmd = info[0];
			return cmd;
		} catch (GException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 获取ps
	 * 
	 * @param uri
	 * @return
	 */
	public static String[] getPs(String uri) {
		try {
			String par = parseURI(uri, Constants.PARAMETER_PLACE);
			if (par.split("[?]").length < 2) {
				return null;
			}
			String[] info = par.split("[?]",2)[1].split("[?]");
			for (int i = 0; i < info.length; i++) {
				info[i] = info[i].trim();
			}
			return info;
		} catch (GException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 获取pid
	 * 
	 * @param uri
	 * @return
	 */
	public static String getPid(String uri) {
		try {
			String oid = parseURI(uri, Constants.OID_PLACE);
			return oid;
		} catch (GException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 获取sid
	 * 
	 * @param uri
	 * @return
	 */
	public static String getSid(String uri) {
		try {
			String sid = parseURI(uri, Constants.SID_PLACE);
			return sid;
		} catch (GException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 根据返回获得参数
	 * 
	 * @param result
	 * @return
	 */
	public static String getPar(ObjMessage result) {
		return "";
	}
}
